package com.jsu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jsu.mapper.ActivityFlowMapper;
import com.jsu.mapper.FlowMapper;
import com.jsu.mapper.FlowSpeakerMapper;
import com.jsu.mapper.SpeakerMapper;
import com.jsu.service.FlowService;
import lombok.extern.slf4j.Slf4j;
import org.example.model.activity.dto.AcFlowWithSpeakerDTO;
import org.example.model.activity.dto.ActFlowDTO;
import org.example.model.activity.dto.FlowDTO;
import org.example.model.activity.dto.FlowWithSpeakerDTO;
import org.example.model.activity.pojo.ActivityFlow;
import org.example.model.activity.pojo.Flow;
import org.example.model.activity.pojo.FlowSpeaker;
import org.example.model.common.dtos.ResponseResult;
import org.example.model.user.pojo.Speaker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FlowServiceImpl implements FlowService {
    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private ActivityFlowMapper activityFlowMapper;
    @Autowired
    private FlowSpeakerMapper flowSpeakerMapper;
    @Override
    public ResponseResult getAllFlows() {
        LambdaQueryWrapper<Flow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Flow> flowList = flowMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(flowList);
    }

    @Override
    public ResponseResult getFlowByAgendaId(Integer id) {
        LambdaQueryWrapper<Flow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Flow::getAgendaId,id);
        List<Flow> flowList = flowMapper.selectList(lambdaQueryWrapper);

        return ResponseResult.okResult(flowList);
    }

    @Override
    public ResponseResult updateFlow(FlowWithSpeakerDTO flowInfo) {
        Flow flow = new Flow();
        BeanUtils.copyProperties(flowInfo,flow);
        log.info("flowInfo:{}",flowInfo);
        if (Objects.isNull(flowInfo)){
            return ResponseResult.errorResult(401,"flowInfo is null");
        }
        //先更新flow表
        flowMapper.updateById(flow);
        //再更新flow_speaker表
        //先删除flowId对应的记录
        flowSpeakerMapper.delete(new LambdaQueryWrapper<FlowSpeaker>().eq(FlowSpeaker::getFlowId,flowInfo.getPkId()).eq(FlowSpeaker::getType,1));
        //再插入新的记录
        for (Speaker speaker : flowInfo.getSpeakers()) {
            FlowSpeaker flowSpeaker = new FlowSpeaker();
            flowSpeaker.setFlowId(flowInfo.getPkId());
            flowSpeaker.setSpeakerId(speaker.getPkId());
            flowSpeaker.setType(1);
            flowSpeakerMapper.insert(flowSpeaker);
        }
        return ResponseResult.okResult(flowInfo);
    }

    @Override
    public ResponseResult deleteFlow(Integer flowId) {
        log.info("flowId:{}",flowId);
        if (Objects.isNull(flowId)){
            return ResponseResult.errorResult(401,"flowInfo is null");
        }
        Flow flow = flowMapper.selectById(flowId);

        if (Objects.isNull(flow)){
            log.info(flow.toString());
        }
        int count = flowMapper.deleteById(flowId);
        if (count == 0){
            return ResponseResult.errorResult(402,"删除失败");
        }

        // 删除flow_speaker表中对应flowId的记录
        flowSpeakerMapper.delete(new LambdaQueryWrapper<FlowSpeaker>().eq(FlowSpeaker::getFlowId,flowId)
                .eq(FlowSpeaker::getType,1));

        return ResponseResult.okResult(200,"删除成功");
    }

    @Override
    public ResponseResult addFlow(FlowDTO flowInfo) {
        Flow flow = new Flow();
        BeanUtils.copyProperties(flowInfo,flow);
        log.info("flowInfo:{}",flowInfo);
        if (Objects.isNull(flowInfo)){
            return ResponseResult.errorResult(401,"flowInfo is null");
        }

        flowMapper.insert(flow);

        for (Integer speakerId : flowInfo.getSpeakerList()) {
            FlowSpeaker speakerFlow = new FlowSpeaker();
            speakerFlow.setFlowId(flow.getPkId());
            speakerFlow.setSpeakerId(speakerId);
            speakerFlow.setType(1);
            flowSpeakerMapper.insert(speakerFlow);
        }

        return ResponseResult.okResult(200,"添加成功");
    }

    @Override
    public ResponseResult getAllFlowsAct() {
        LambdaQueryWrapper<ActivityFlow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<ActivityFlow> flowList = activityFlowMapper.selectList(lambdaQueryWrapper);
        return ResponseResult.okResult(flowList);
    }

    @Override
    public ResponseResult updateFlowAct(AcFlowWithSpeakerDTO flowInfo) {
        ActivityFlow flow = new ActivityFlow();
        BeanUtils.copyProperties(flowInfo,flow);
        log.info("flowInfo:{}",flowInfo);
        //先更新flow表
        activityFlowMapper.updateById(flow);
        //再更新flow_speaker表
        //先删除flowId对应的记录
        flowSpeakerMapper.delete(new LambdaQueryWrapper<FlowSpeaker>().eq(FlowSpeaker::getFlowId,flowInfo.getPkId()).eq(FlowSpeaker::getType,2));
        //再插入新的记录
        for (Speaker speaker : flowInfo.getSpeakers()) {
            FlowSpeaker flowSpeaker = new FlowSpeaker();
            flowSpeaker.setFlowId(flowInfo.getPkId());
            flowSpeaker.setSpeakerId(speaker.getPkId());
            flowSpeaker.setType(2);
            flowSpeakerMapper.insert(flowSpeaker);
        }
        return ResponseResult.okResult(flowInfo);
    }

    @Override
    public ResponseResult deleteFlowAct(Integer flowId) {
        log.info("flowId:{}",flowId);
        if (Objects.isNull(flowId)){
            return ResponseResult.errorResult(401,"flowInfo is null");
        }
        ActivityFlow flow = activityFlowMapper.selectById(flowId);

        if (Objects.isNull(flow)){
            log.info(flow.toString());
        }
        int count = activityFlowMapper.deleteById(flowId);
        if (count == 0){
            return ResponseResult.errorResult(402,"删除失败");
        }

        // 删除flow_speaker表中对应flowId的记录
        flowSpeakerMapper.delete(new LambdaQueryWrapper<FlowSpeaker>().eq(FlowSpeaker::getFlowId,flowId)
                .eq(FlowSpeaker::getType,2));

        return ResponseResult.okResult(200,"删除成功");
    }

    @Override
    public ResponseResult addFlowAct(ActFlowDTO flowInfo) {
        ActivityFlow flow = new ActivityFlow();
        BeanUtils.copyProperties(flowInfo,flow);
        log.info("flowInfo:{}",flowInfo);

        activityFlowMapper.insert(flow);
        //插入flow_speaker表
        for (Integer speakerId : flowInfo.getSpeakerList()) {
            FlowSpeaker speakerFlow = new FlowSpeaker();
            speakerFlow.setFlowId(flow.getPkId());
            speakerFlow.setSpeakerId(speakerId);
            speakerFlow.setType(2);
            flowSpeakerMapper.insert(speakerFlow);
        }

        return ResponseResult.okResult(200,"添加成功");
    }


}
